package com.pickgliss.ui.image
{
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.ObjectUtils;
	import com.pickgliss.utils.StringUtils;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;

	/**
	 * 
	 * @author Herman
	 * 9宫格拉伸的图片，但是支持跳帧，每一帧的图片都不一样
	 * 
	 */	
	public class ScaleFrameImage extends Image
	{
		public static const P_fillAlphaRect:String = "fillAlphaRect";

		public function ScaleFrameImage(){}

		private var _currentFrame:uint;
		private var _fillAlphaRect:Boolean;
		private var _imageLinks:Array;
		private var _images:Vector.<DisplayObject>;

		override public function dispose():void
		{
			removeImages();
			_images = null;
			_imageLinks = null;
			super.dispose();
		}
		public function set fillAlphaRect(value:Boolean):void
		{
			if(_fillAlphaRect == value)return;
			_fillAlphaRect = value;
			onPropertiesChanged(P_fillAlphaRect);
		}
		
		public function get getFrame():uint
		{
			return _currentFrame;
		}
		
		override public function setFrame(frameIndex:int):void
		{
			if(!_images)return;
			super.setFrame(frameIndex);
			_currentFrame = frameIndex;
			for(var i:int = 0;i<_images.length;i++)
			{
				if(_images[i] == null ) continue;
				if((frameIndex - 1) == i)
				{
					_images[i].visible = true;
					if(_images[i] is MovieClip)
					{
						(_images[i] as MovieClip).play();
					}
					else if(_images[i] is MovieImage)
					{
						(_images[i] as MovieImage).movie.play();
					}
//					if(_width != Math.round(_images[i].width))
//					{
//						_width = Math.round(_images[i].width);
//						_changedPropeties[Component.P_width] = true;
//					}
				}else
				{
					_images[i].visible = false;
					if(_images[i] is MovieClip)
					{
						(_images[i] as MovieClip).gotoAndStop(1);
					}
					else if(_images[i] is MovieImage)
					{
						(_images[i] as MovieImage).movie.gotoAndStop(1);
					}
				}
			}
			fillRect();
		}
		
		private function fillRect():void
		{
			if(_fillAlphaRect)
			{
				graphics.beginFill(0xFF00FF,0);
				graphics.drawRect(0,0,_width,_height);
				graphics.endFill();
			}
		}
		
		override protected function init():void
		{
			super.init();
		}
		
		override protected function resetDisplay():void
		{
			_imageLinks = ComponentFactory.parasArgs(_resourceLink);
			removeImages();
			creatImages();
		}
		
		override protected function updateSize():void
		{
			if(_images == null) return;
			if(_changedPropeties[Component.P_width] || _changedPropeties[Component.P_height])
			{
				for(var i:int = 0;i<_images.length;i++)
				{
					if(_images[i] == null) continue;
					_images[i].width = _width;
					_images[i].height = _height;
				}
			}
		}
		
		private function creatImages():void
		{
			_images = new Vector.<DisplayObject>;
			for(var i:int = 0;i<_imageLinks.length;i++)
			{
				if(StringUtils.isEmpty(_imageLinks[i]))
				{
					_images.push(null);
				}else
				{
					var image:DisplayObject = ComponentFactory.Instance.creat(_imageLinks[i]);
					_width = Math.max(_width,image.width);
					_height = Math.max(_height,image.height);
					_images.push(image);
					addChild(image);
				}
			}
		}
		
		public function getFrameImage(frameIndex:int):DisplayObject
		{
			return _images[frameIndex];
		}
		
		private function removeImages():void
		{
			if(_images == null) return;
			for each (var image:DisplayObject in _images)
			{
				image = _images.pop();
				ObjectUtils.disposeObject(image);
				image = null;
			}
		}
		
		public function get totalFrames():int
		{
			return _images.length;
		}
	}
}